<template>
  <h1>员工列表练习</h1>
  <el-form style="width: 600px;margin: 0 auto;">
    <el-form-item label="姓名">
      <el-input placeholder="请输入员工姓名" v-model="emp.name"></el-input>
    </el-form-item>
    <el-form-item label="工资">
      <el-input placeholder="请输入员工工资" v-model="emp.salary"></el-input>
    </el-form-item>
    <el-form-item label="岗位">
      <el-input placeholder="请输入员工岗位" v-model="emp.job"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="success" @click="addEmp">点我提交</el-button>
    </el-form-item>
  </el-form>

  <el-table :data="arr" style="width: 600px;margin: 0 auto;">
    <el-table-column label="编号" type="index" align="center" width="90"></el-table-column>
    <el-table-column label="姓名" prop="name" align="center"></el-table-column>
    <el-table-column label="工资" prop="salary" align="center"></el-table-column>
    <el-table-column label="岗位" prop="job" align="center"></el-table-column>
    <el-table-column label="操作" align="center">
      <template #default="scope">
        <el-button type="danger" @click="del(scope.row,scope.$index)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script setup>
import {ref} from "vue";
import {ElMessage} from "element-plus";

//定义对象用来保存表单双向绑定收集到的数据
const emp = ref({name:'',salary:'',job:''});
//定义数组用来保存员工表格数据
const arr = ref([]);
//定义保存员工额方法
const addEmp = ()=>{
  //对表单数据做非空校验
  if(emp.value.name.trim()=='' || emp.value.salary.trim()=='' || emp.value.job.trim()==''){
    ElMessage.error('请输入完整的员工信息!');
    return;
  }
  //将收集到的员工数据添加到数组中
  arr.value.push(emp.value);
  //表单双向绑定数据用完后一定要清空!
  emp.value = {name:'',salary:'',job:''};
  console.log(arr.value);
}
//定义删除员工的方法
const del = (row,index)=>{
  if(confirm('您确认要删除'+row.name+'吗?')){
    arr.value.splice(index,1);
    ElMessage.success('删除成功!');
  }
}
</script>

<style scoped>

</style>